package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.BusinessException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired  //依赖注入
    private CategoryMapper categoryMapper;

    /**
     * 分页列表查询
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageBean findByPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        //1 开启分页
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());

        //2 执行查询
        List<Category> list = categoryMapper.findByPage(categoryPageQueryDTO);
        Page<Category> page = (Page<Category>) list;

        //3 返回结果
        return new PageBean(page.getTotal(),page.getResult());
    }

    /**
     *新增分类
     * @param category
     */
    @Override
    public void save(Category category) {

        //1 补齐参数  (通用字段 在Mapper中写上自定义注解)
        category.setStatus(1);//状态默认启用(上架)

        //2 调用Mapper
        categoryMapper.save(category);

    }


    /**
     * 删除分类
     * @param id
     */

    @Autowired
    private DishMapper dishMapper; //dishMapper依赖注入

    @Autowired
    private SetmealMapper setmealMapper; //setmealMapper依赖注入
    @Override
    public void delete(Long id) {
        //1 查询 当前id分类下是否还有菜品,有则报错
        int count1 = dishMapper.countByCategoryId(id);
        if (count1 > 0) {
            throw new BusinessException("当前分类下有菜品,无法删除");
        }
        //2 查询 当前id分类下是否还有套餐,有则报错
        int count2 = setmealMapper.countByCategoryId(id);
        if (count2 > 0) {
            throw new BusinessException("当前分类下有套餐,无法删除");
        }
        //3 删除分类
        categoryMapper.delete(id);
    }

    /**
     * 修改分类
     * @param category
     */
    @Override
    public void update(Category category) {
        //1 补齐参数  (使用自定义注解 自动填充 补全参数 )

        //2 调用Mapper
        categoryMapper.update(category);
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> findByType(Integer type) {

        //1 将参数封装到dto中
        CategoryPageQueryDTO dto = new CategoryPageQueryDTO();
        dto.setType(type);

        //2 调用查询方法
        return categoryMapper.findByPage(dto);
    }

    /**
     * 分类列表查询(小程序)
     * @param type
     * @return
     */
    @Override
    public List<Category> findByStatusAndType(Integer type) {
        //1 封装到DTO
        CategoryDTO dto = new CategoryDTO();
        dto.setType(type);// 类型
        dto.setStatus(1);// 展示出来的需是在售,所以状态需为1
       /*1. 封装dto
        CategoryDTO categoryDTO = CategoryDTO.builder()
                .status(1)
                .type(type)
                .build();*/
        //2 调用查询方法Mapper
        return categoryMapper.findByStatusAndType(dto);

    }


    /**
     * 启用禁用分类
     * @param category
     */
    @Override
    public void startOrStop(Category category) {
        //1 调用Mapper (///起售停售本身也算是修改(直接复用))
        categoryMapper.update(category);

    }


}
